//
//  JCCMoneyViewController.swift
//  剂查查
//
//  Created by shoubin tao on 2024/12/3.
//  Copyright © 2024 shipin. All rights reserved.
//

import Foundation
import UIKit


class JCCMoneyModel {
    var id: Int = 0
    var uid: Int = 0
    var balance:Int = 0
    var cash_money:Int = 0
    var use_cash_money:Int = 0
    var freeze_money:Int = 0
    init(dict: [String: Any]) {
        self.id = dict["id"] as? Int ?? 0
        self.uid = dict["uid"] as? Int ?? 0
        self.balance = dict["balance"] as? Int ?? 0
        self.cash_money = dict["cash_money"] as? Int ?? 0
        self.use_cash_money = dict["use_cash_money"] as? Int ?? 0
        self.freeze_money = dict["freeze_money"] as?  Int ?? 0
    }
}

class JCCRedBagModel {
    var amount: String = ""
    var create_at:Int = 0
    var goods:[String:Any] = [:]
    var status:String = ""
    init(dict: [String: Any]) {
        self.amount = dict["amount"] as? String ?? ""
        self.goods = dict["goods"] as? [String:Any ] ?? [:]
        self.status = dict["balance"] as? String ?? ""
        self.create_at = dict["create_at"] as? Int ?? 0
    }
}

class JCCApplyMoneyModel {
    var id: Int = 0
    var wechat_order_id : String = ""
    var uid : Int = 0
    var real_name :String = ""
    var extract_type: String = ""
    var bank_code : String = ""
    var bank_address :String = ""
    var alipay_code : String = ""
    var extract_price : String = ""
    var mark : String = "呵呵"
    var balance: String = "100.00"
    var fail_msg: String = "绝对不给"
    var fail_time: Int = 0
    var add_time: Int = 0
    var status: Int = 0
    var wechat: String = "sj969423691"
    var qrcode_url: String = ""
    var money_status: Int = 0
//    "user": {
//    "id": 10028,
//    "nickname": "水泥哥哥"
    }
    var id: Int = 0
    var uid: Int = 0
    var balance:Int = 0
    var cash_money:Int = 0
    var use_cash_money:Int = 0
    var freeze_money:Int = 0

//    init(dict: [String: Any]) {
//        self.id = dict["id"] as? Int ?? 0
//        self.wechat_order_id = dict["wechat_order_id"] as? String ?? ""
//        self.balance = dict["balance"] as? Int ?? 0
//        self.cash_money = dict["cash_money"] as? Int ?? 0
//        self.use_cash_money = dict["use_cash_money"] as? Int ?? 0
//        self.freeze_money = dict["freeze_money"] as?  Int ?? 0
//    }



class JCCMoneyTableViewCell: UITableViewCell {
    @IBOutlet weak var moneyLabel: UILabel!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var statusLabel: UILabel!
    
    @IBOutlet weak var numberLabel: UILabel!
    override func layoutSubviews() {
    }
}

class JCCMoneyViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    @IBOutlet weak var currentMoneyLabel: UILabel!
    
    @IBOutlet weak var allApplyMoneyLabel: UILabel!
    @IBOutlet weak var allMoneyLabel: UILabel!
    var currentModel:JCCMoneyModel = JCCMoneyModel(dict: [:])
    @IBOutlet weak var waitPayMoneyLabel: UILabel!
    
    @IBOutlet weak var applyBtn: UIButton!
    @IBOutlet weak var moneyTableView: UITableView!
    var redBagModels:[JCCRedBagModel] = [JCCRedBagModel(dict: [:])]
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        return self.redBagModels.count
    }
    override func viewWillAppear(_ animated: Bool) {
        
        self.navigationController?.navigationBar.isHidden = true
    }
    
    
    
    override func viewWillDisappear(_ animated: Bool) {
        self.navigationController?.navigationBar.isHidden = false
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:JCCMoneyTableViewCell = tableView.dequeueReusableCell(withIdentifier: "moneyCell", for: indexPath) as! JCCMoneyTableViewCell
        let redBagModel:JCCRedBagModel = self.redBagModels[indexPath.row]
        cell.moneyLabel.text = redBagModel.amount
        let time:TimeInterval = TimeInterval(redBagModel.create_at)
        cell.numberLabel.text = time.dateStr()
        cell.titleLabel.text = redBagModel.goods["name"] as? String
        cell.statusLabel.text = " \(getStatus(statusIndex: redBagModel.status)) "
        cell.statusLabel.backgroundColor = UIColor.init(hex:getStatusColor(statusIndex: redBagModel.status))
        cell.statusLabel.layer.cornerRadius = 6
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 77
    }
    override func viewDidLoad() {
        self.moneyTableView.delegate = self
        self.moneyTableView.dataSource = self
        self.moneyTableView.separatorStyle = .none
        self.applyBtn.layer.cornerRadius = 17
        getData()
    }
    func updateUI() {
        self.allMoneyLabel.text = "\(self.currentModel.balance)"
        self.allApplyMoneyLabel.text = "\(self.currentModel.use_cash_money)"
        self.waitPayMoneyLabel.text = "\(self.currentModel.freeze_money)"
        self.currentMoneyLabel.text = "\(self.currentModel.cash_money)"
    }
    func getStatus(statusIndex:String) -> String {
        if (statusIndex == "0")
        {
            return "待结算";
        }else if (statusIndex == "1"){
            return "已结算";
        }else {
            return "照片不合格";
        }
    }
    
    func getStatusColor(statusIndex:String) -> String {
        if (statusIndex == "0")
        {
            return "#A6A6A6";
        }else if (statusIndex == "1"){
            return "#FF5733";
        }else {
            return "#EFC7AA";
        }
    }
    
    func getData () {
        JCCNet.getUserMoney(completion: {
            model,error in
            
            if(model?.isSuccess == true){
                let dicts = model?.result as! [String:Any]
                let model:JCCMoneyModel = JCCMoneyModel(dict:dicts)
                self.currentModel = model
                self.updateUI()
            }else {
                EZLoadingActivity.Settings.SuccessText = model?.info ?? ""
                EZLoadingActivity.hide(true, animated: false)
            }
        })
        
        JCCNet.getRedBagRecord(completion: {
            model,error in
            
            if(model?.isSuccess == true){
                let dicts = model?.result as! [String:Any]
                let count = dicts["count"] as? Int
                if (count  == 0) {
                    self.redBagModels = []
                }else {
                    let listDict = dicts["list"] as! [[String:Any]]
                    let models:[JCCRedBagModel] = listDict.map { JCCRedBagModel(dict: $0 ) }
                    self.redBagModels = models
                }
                self.moneyTableView.reloadData()
            }else {
                EZLoadingActivity.Settings.SuccessText = model?.info ?? ""
                EZLoadingActivity.hide(true, animated: false)
            }
        })
    }
}
